﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Data
{
    public class Combine
    {
        public ArrayList combine(int[] a, int m)
        {

            int n = a.Length;
            if (m > n)
            {
                Console.Write(1111);
            }

            ArrayList result = new ArrayList();
            int[] bs = new int[n];
            for (int i = 0; i < n; i++)
            {
                bs[i] = 0;
            }
            //初始化   
            for (int i = 0; i < m; i++)
            {
                bs[i] = 1;
            }
            bool flag = true;
            bool tempFlag = false;
            int pos = 0;
            int sum = 0;
            //首先找到第一个10组合，然后变成01，同时将左边所有的1移动到数组的最左边   
            do
            {
                sum = 0;
                pos = 0;
                tempFlag = true;
                result.Add(print(bs, a, m));

                for (int i = 0; i < n - 1; i++)
                {
                    if (bs[i] == 1 && bs[i + 1] == 0)
                    {
                        bs[i] = 0;
                        bs[i + 1] = 1;
                        pos = i;
                        break;
                    }
                }
                //将左边的1全部移动到数组的最左边   

                for (int i = 0; i < pos; i++)
                {
                    if (bs[i] == 1)
                    {
                        sum++;
                    }
                }
                for (int i = 0; i < pos; i++)
                {
                    if (i < sum)
                    {
                        bs[i] = 1;
                    }
                    else {
                        bs[i] = 0;
                    }
                }

                //检查是否所有的1都移动到了最右边   
                for (int i = n - m; i < n; i++)
                {
                    if (bs[i] == 0)
                    {
                        tempFlag = false;
                        break;
                    }
                }
                if (tempFlag == false)
                {
                    flag = true;
                }
                else {
                    flag = false;
                }

            } while (flag);
            result.Add(print(bs, a, m));

            return result;
        }

        private int[] print(int[] bs, int[] a, int m)
        {
            int[] result = new int[m];
            int pos = 0;
            for (int i = 0; i < bs.Length; i++)
            {
                if (bs[i] == 1)
                {
                    result[pos] = a[i];
                    pos++;
                }
            }
            return result;
        }

        public string print(ArrayList l,int num)
        {
            ArrayList arrlst = new ArrayList();
            string redBall = "";
            string aa = "";
            string bb = "";
            //for (int k = 1; k < 17; k++)
            //{
            for (int i = 0; i < l.Count; i++)
            {
                if (l.Count == 2 && i == 1) break;
                int[] a = (int[])l[i];
                redBall = "";
                for (int j = 0; j < a.Length; j++)
                {
                    aa = a[j].ToString();
                    if (a[j] <= 9)
                    {
                        aa = "0" + a[j];
                    }
                    if (j >= num-1)
                    {
                        //Response.Write(aa);
                        redBall += aa;
                    }
                    else
                    {
                        //Response.Write(aa + ",");
                        redBall += aa + ",";

                    }
                    //arrlst.Add("|");

                }
                //Response.Write("|");
                //if (k <= 9)
                //{
                //    bb = "0" + k;
                //}
                //else
                //{
                //    bb = k.ToString();
                //}
                //Data.LotteryAnnouncement.Inert(redBall);
                bb += redBall + "|";
                //DoubleBall dd = new DoubleBall();
                //Model.DoubleBall mod = new Model.DoubleBall();
                //mod.Larr = redBall;
                //  dd.Insert(mod);

                //}
            }
            //if (l.Count == 2)
                //Response.Write(l.Count - 1);
            //else
                //Response.Write(l.Count);
            return bb;

        }


        //public void printAnyThree()
        //{
        //    int[] num = new int[] { 5 };
        //    num = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
        //           27, 28, 29, 30, 31, 32, 33 };
        //    //Response.Write("开始时间：" + DateTime.Now.TimeOfDay.ToString());

        //    print(combine(num, 5));

        //}
    }
}
